<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../assets/mock/mock-min.js"></script>
    <script>
        console.log(Mock.mock({
            tel1: /^(0|86|17951)?(1[0-9]{10})$/,
            tel2: /^(1[0-9]{10})$/,
            idCard: /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/,
            email: /[a-zA-Z0-9]{5,}@[a-z]{3,6}\.(com|cn|org|net)/
        }))

        // // 成功
        // {
        //     state:true,
        //     msg:"",
        //     users:[
        //         {},
        //         {},
        //         {}
        //     ]
        // }
        // // 失败
        // {
        //     state:false,
        //     msg:"服务器查询数据失败",
        //     users:null
        // }

        // mock的属性取值方法，是用于扩展随机生成的方法规则，通过 return 结果描述生成值
        //        该方法的this将自动指向于生成的模拟数据对象
        console.log(Mock.mock({
            callback: function () {
                let n = Math.random();
                if (n >= 0.5) {
                    return 1
                } else {
                    return 2
                }
            }
        }))

        console.log(Mock.mock({
            users: function () {
                let n = Math.random();
                if (n >= 0.5) {
                    return [
                        {
                            name: "tom",
                            age: 23,
                            tel: 123123123123
                        }
                    ]
                } else {
                    return null
                }
            }
        }))

        console.log(Mock.mock({
            "state|1": true,
            msg: "消息",
            users: function () {
                let n = Math.random();
                if (n >= 0.5) {
                    let tempObj = Mock.mock({
                        "users|10": [
                            {
                                name: "@cname",
                                "age|1-100": 0,
                                tel: /^(1[0-9]{10})$/,
                            }
                        ]
                    })
                    return tempObj.users
                } else {
                    return null
                }
            }
        }))

        console.log(Mock.mock({
            "state|9-1": true,
            msg: "消息",
            users: function () {
                if (this.state) {
                    let tempObj = Mock.mock({
                        "users|10": [
                            {
                                name: "@cname",
                                "age|1-100": 0,
                                tel: /^(1[0-9]{10})$/,
                            }
                        ]
                    })
                    return tempObj.users
                } else {
                    return null
                }
            }
        }))


        // path 路径指向，参考文件路径格式，以路径层级方式获取已生成的模拟数据属性值
        console.log( Mock.mock({
            "foo": "Hello",
            "nested": {
                "a": {
                    "b": {
                        "c": "@cname"
                    }
                }
            },
            // @/ 表示mock生成的根对象数据
            "arg": "@/foo @/nested/a/b/c",
            obj:{
                // @../  表示以当前属性层级为参考，进行取值的顺序后退
                arg:"@../foo @../nested/a/b/c"
            }
        }) )
    </script>
</head>

<body>

</body>

</html>